<template>
    <div>
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="网关" prop="gatewayId">
                <gateway-select @changeGateway="changeGateway"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-button
            plain
            :disabled="multiple"
            @click="processHandler"
        >设为已处理
        </el-button>

        <el-table v-loading="loading" :data="gatewayExpireList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="序号" align="center" type="index" :index="calculateIndex"/>
            <el-table-column label="网关名称" align="center" prop="gatewayName"/>
            <el-table-column label="网关编号" align="center" prop="gatewayNumber"/>
            <el-table-column label="通知时间" align="center" prop="noticeTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.noticeTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="是否通知" align="center" prop="status">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.status==='0'" type="info">否</el-tag>
                    <el-tag v-if="scope.row.status==='1'" type="success">是</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="到期时间" align="center" prop="expireTime">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.expireTime) }}</span>
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />
    </div>
</template>

<script>
import {delGatewayExpire, getGatewayExpire, listGatewayExpire, process} from "@/api/report/gatewayExpire"

import gatewaySelect from '@/views/common/GatewaySelect.vue'

export default {
    name: "GatewayExpire",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 网关到期消息表格数据
            gatewayExpireList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                gatewayId: null,
                noticeTime: null,
                status: null,
                expireTime: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {}
        }
    },
    created() {
        this.getList()
    },
    components: {
        gatewaySelect
    },
    methods: {

        // 接收子组件传递的消息
        changeGateway(obj) {
            this.queryParams.gatewayId = obj.gatewayId
            this.getList()
        },

        calculateIndex(index) {
            const pageNum = this.queryParams.pageNum
            const pageSize = this.queryParams.pageSize
            return index + 1 + (pageNum - 1) * pageSize
        },

        getList() {
            this.loading = true
            listGatewayExpire(this.queryParams).then(response => {
                this.gatewayExpireList = response.rows
                this.total = response.total
                this.loading = false
            })
        },

        cancel() {
            this.open = false
            this.reset()
        },

        reset() {
            this.form = {
                id: null,
                projectId: null,
                gatewayId: null,
                noticeTime: null,
                status: "0",
                expireTime: null
            };
            this.resetForm("form")
        },

        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },

        resetQuery() {
            this.resetForm("queryForm")
            this.handleQuery()
        },

        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },

        handleAdd() {
            this.reset()
            this.open = true
            this.title = "添加网关到期消息"
        },

        handleUpdate(row) {
            this.reset()
            const id = row.id || this.ids
            getGatewayExpire(id).then(response => {
                this.form = response.data
                this.open = true
                this.title = "修改网关到期消息"
            })
        },

        processHandler() {
            process({ids: this.ids}).then(response => {
                this.$message.success('成功')
                this.getList()
                this.$emit('getCount')
            })
        },

        handleDelete(row) {
            const ids = row.id || this.ids
            this.$modal.confirm('是否确认删除网关到期消息编号为"' + ids + '"的数据项？').then(function () {
                return delGatewayExpire(ids)
            }).then(() => {
                this.getList()
                this.$modal.msgSuccess("删除成功")
            }).catch(() => {
            })
        }
    }
}
</script>
